﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI;

using WebSite.Domain;

namespace WebSite.DAL {
    public class ItemProntuarioDAO 
    {

        public static List<ItemProntuario> ListarItensProntuario()
        {
            SqlDataReader dr;
            string strsql = "SELECT * FROM tab_ItemProntuario";
            SqlConnection objConnection = ConnectionFactory.obterConexao();

            List<ItemProntuario> itensProntuario;

            try
            {
                objConnection.Open();
                SqlCommand objCommand = new SqlCommand(strsql, objConnection);
                

                dr = objCommand.ExecuteReader();
                if (dr.HasRows)
                {
                    itensProntuario = new List<ItemProntuario>();

                    while (dr.Read())
                    {
                        itensProntuario.Add(new ItemProntuario(int.Parse(dr["idItemProntuario"].ToString()), dr["Nome"].ToString(), int.Parse(dr["TipoContinuo"].ToString())));
                    }
                }
                else
                {
                    itensProntuario = null;
                }

                return itensProntuario;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                objConnection.Close();

            }
        }

        public static void BuscarItensDeProntuario(int idProntuario, List<ItemProntuario> itensProntuario)
        {
            SqlDataReader dr;
            string strsql =   "SELECT *" 
                            + " FROM     tab_ItemProntuario INNER JOIN"
                            + " tab_ItemProntuarioPorProntuario ON dbo.tab_ItemProntuario.idItemProntuario = tab_ItemProntuarioPorProntuario.idItemProntuario INNER JOIN"
                            + " tab_Prontuario ON tab_ItemProntuarioPorProntuario.idProntuario = tab_Prontuario.idProntuario "
                            + " WHERE tab_ItemProntuarioPorProntuario.idProntuario = @idProntuario";
            SqlConnection objConnection = ConnectionFactory.obterConexao();

           // List<ItemProntuario> ips = new List<ItemProntuario>();

            try
            {
                objConnection.Open();
                SqlCommand objCommand = new SqlCommand(strsql, objConnection);
                objCommand.Parameters.Add(new SqlParameter("@idProntuario", idProntuario));

                dr = objCommand.ExecuteReader();               

                while (dr.Read())
                {
                    itensProntuario.Add(new ItemProntuario(int.Parse(dr["idItemProntuario"].ToString()), dr["Nome"].ToString(), int.Parse(dr["TipoContinuo"].ToString())));                       
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {                
                objConnection.Close();
            }            
        }

        public static void CadastrarItemProntuario(ItemProntuario itemProntuario)
        {
            string strsql = "INSERT INTO tab_ItemProntuario(Nome, TipoContinuo) VALUES (@Nome,@TipoContinuo);";
            SqlConnection objConnection = ConnectionFactory.obterConexao();
            objConnection.Open();
            SqlCommand objCommand = new SqlCommand(strsql, objConnection);
            objCommand.Parameters.Add(new SqlParameter("@Nome", itemProntuario.nome));
            objCommand.Parameters.Add(new SqlParameter("@TipoContinuo", itemProntuario.tipoContinuo));            
            objCommand.ExecuteNonQuery();
            objConnection.Close();
        }
    }
}